//
//  UILabel+Common.m
//  beichoo_N_ios
//
//  Created by 陈健 on 2017/8/22.
//  Copyright © 2017年 陈健. All rights reserved.
//

#import "UILabel+Common.h"
#import "UIColor+Expand.h"

@implementation UILabel (Common)

+ (instancetype)labelWithFont:(UIFont *)font textColor:(UIColor *)textColor {
    UILabel *label = [self new];
    label.font = font;
    label.textColor = textColor;
    return label;
}

+ (instancetype)labelWithSystemFontSize:(CGFloat)fontSize textColorHexString:(NSString *)stringToConvert {
    UILabel *label = [self new];
    label.font = [UIFont systemFontOfSize:fontSize];
    label.textColor = [UIColor colorWithHexString:stringToConvert];
    return label;
}

- (void)addAttrDict:(NSDictionary *)attrDict toStr:(NSString *)str {
    if (str.length <= 0) {
        return;
    }
    NSMutableAttributedString *attrStr = self.attributedText? self.attributedText.mutableCopy: [[NSMutableAttributedString alloc] initWithString:self.text];
    [self addAttrDict:attrDict toRange:[attrStr.string rangeOfString:str]];
}

- (void)addAttrDict:(NSDictionary *)attrDict toRange:(NSRange)range {
    if (range.location == NSNotFound || range.length <= 0) {
        return;
    }
    NSMutableAttributedString *attrStr = self.attributedText? self.attributedText.mutableCopy: [[NSMutableAttributedString alloc] initWithString:self.text];
    if (range.location + range.length > attrStr.string.length) {
        return;
    }
    [attrStr addAttributes:attrDict range:range];
    self.attributedText = attrStr;
}

- (void)colorTextWithColor:(UIColor *)color range:(NSRange)range {
    NSMutableAttributedString *attrStr = self.attributedText? self.attributedText.mutableCopy: [[NSMutableAttributedString alloc] initWithString:self.text];
    
    [attrStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    self.attributedText = attrStr;
}

- (void)fontTextWithFont:(UIFont *)font range:(NSRange)range {
    NSMutableAttributedString *attrStr = self.attributedText? self.attributedText.mutableCopy: [[NSMutableAttributedString alloc] initWithString:self.text];
    
    [attrStr addAttribute:NSFontAttributeName value:font range:range];
    self.attributedText = attrStr;
}



-(void)SendLabelColor:(UIColor *)labelColor
     andLabelFontSize:(CGFloat)labelSize
         andLabelText:(NSString *)labelString
{
    self.text = labelString;
    self.textColor = labelColor;
    //    self.font = [UIFont systemFontOfSize:labelSize];
    self.font = [UIFont systemFontOfSize:labelSize];
}

- (void)configLableValue:(NSString *)achievementString andLineSpace:(NSInteger)lineSpace{
    if ([achievementString length] == 0) {
        return;
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:achievementString];
    NSMutableParagraphStyle *paragrphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragrphStyle setLineSpacing:lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragrphStyle range:NSMakeRange(0, achievementString.length)];
    self.attributedText = attributedString;
}
- (void)configLableValueOfutableAttributedStr:(NSMutableAttributedString *)achievementString andLineSpace:(NSInteger)lineSpace
{
    if ([achievementString length] == 0) {
        return;
    }
    NSMutableAttributedString *attributedString = achievementString;
    NSMutableParagraphStyle *paragrphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragrphStyle setLineSpacing:lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragrphStyle range:NSMakeRange(0, achievementString.length)];
    self.attributedText = attributedString;
}

+ (CGSize)sizeLabelText:(NSString *)text width:(CGFloat)width {
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, MAXFLOAT)];
    lab.numberOfLines = 0;
    lab.attributedText = [self attributedStringWithHTMLString:text color:[UIColor yellowColor] font:14];
    CGSize labSize = [lab sizeThatFits:lab.bounds.size];
    return labSize;
}

+ (NSMutableAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString color:(UIColor *)color font:(CGFloat)font
{
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                               NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding)};
    
    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableAttributedString *attributedString = [[[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil] mutableCopy];
    NSMutableParagraphStyle *paragrphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragrphStyle setLineSpacing:4.0];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragrphStyle range:NSMakeRange(0, [attributedString  length])];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:NSMakeRange(0, [attributedString  length])];
    [attributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, [attributedString  length])];
    return attributedString;
}

- (void)configLabelRichTextWithFont:(NSInteger)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor {
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.text];
    //设置字号
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:range];
    //设置文字颜色
    [str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
    self.attributedText = str;
}

@end
